package com.qcrud.core;

import com.qcrud.core.datasource.DbType;
import com.qcrud.tookit.JdbcUtils;
import lombok.Getter;

import java.io.Closeable;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@Getter
public class StatementContext implements Closeable {
    private PreparedStatement preparedStatement;
    private Connection connection;
    private Binding binding = new Binding();
    private DbType dbType;

    public StatementContext(Connection connection) throws SQLException {
        this.connection = connection;
        final DatabaseMetaData metaData = connection.getMetaData();
        this.dbType = JdbcUtils.getDbTypeByUrl(metaData.getURL());
    }

    @Override
    public void close() throws IOException {

    }

    public SqlStatement createSqlStatement(String sql) throws SQLException {
        preparedStatement = connection.prepareStatement(sql);
        return new SqlStatement(this, sql, preparedStatement);
    }

    public Binding getBinding() {
        return this.binding;
    }
}
